#include "view_system.h"
#include "prj_typedef.h"
#include "blecom.h"
#include "display_ctrl.h"
#include "view_abstract.h"
#include "platform_assert.h"
#include "times/view_times.h"
#include "heart_rate/view_heart_rate.h"
#include "others/view_others.h"
#include "metronome/view_metronome.h"
#include "sports/view_sports.h"
#include "relax/view_relax.h"
#include "app_trace.h"
#include "prompt/view_prompt.h"
#include "system/view_system.h"
#include "prj_typedef.h"
#include "motor.h"
#include "prj_config.h"
#include "display_interface_240x240.h"
#include "lfs_file_manager_auto.h"
#include "heart_rate/view_heart_rate.h"
#include "stopwatch.h"
#include "heart_rate_interface.h"
#include "rtc.h"
#include "view_common.h"
#include "font/num_font.h"
#include "app_trace.h"
#include "m_product_test.h"
#include "battery_ctrl.h"
#include "times/view_time_0.h"
#include "blecom.h"
#ifdef UI_BLOOD_PRESSURE
#include "blood_pressure/blood_pressure.h"
#endif
#ifdef UI_BLOOD_OXYGEN
#include "blood_oxygen/blood_oxygen.h"
#endif
#ifdef UI_BLOOD_GLUCOSE
#include "view_blood_glucose/view_blood_glucose.h"
#endif
#ifdef UI_CHOLESTREROL
#include "view_cholestrerol/view_cholestrerol.h"
#endif
#ifdef AXILLARY_TEMPERATURE
#include "body_temperature.h"
#endif
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_power_on_display(void)
{
#if defined(PROJECT_ENERGYFPRO)
    int const auto_refresh_time = 250;
    int const auto_refresh_count = 12;
#elif defined(POWER_ON_ANIMATION_MS)
    int const auto_refresh_time = 300;
    int const auto_refresh_count = POWER_ON_ANIMATION_MS / auto_refresh_time;
#else
    int const auto_refresh_time = 300;
    int const auto_refresh_count = 4;
#endif
    if (disp_ctrl.loop_cnt == 0)
    {

        if (product_test_state_get() != PRODUCT_TEST_STATE_TEST_ASSEM)
        {
            display_show(view_time, DISP_MODE_NORMAL);
            return;
        }

        disp_ctrl.auto_refresh_time = auto_refresh_time;

        if (disp_ctrl.auto_refresh_count >= auto_refresh_count)
        {
            LOG("view_power_on_display %d", disp_ctrl.auto_refresh_count);
            display_show(view_time, DISP_MODE_DOWN_MOVE);
            return;
        }
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
	#warning TODO:
}
void view_power_off_display(void)
{
#if defined(PROJECT_ENERGYFPRO)
    int const auto_refresh_time = 250;
    int const auto_refresh_count = 12;
#else
    int const auto_refresh_time = 300;
    int const auto_refresh_count = 4;
#endif
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = auto_refresh_time;
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
    	#warning TODO:
}
bool view_power_on_event(event_param_t param)
{
#ifdef POWER_ON_MS
#ifdef USE_TOUCHPAD
    if (param.event == BUTTON_EVENT_TP_TAP_LONG)
    {
        power_on();
        motor_start(500);
        display_show(view_power_on_animation, DISP_MODE_NORMAL);
        return true;
    }
#endif
    if (param.event == BUTTON_EVENT_LONG_PRESS)
    {
        return true;
    }
    if (param.event == BUTTON_EVENT_LONG_PRESS_KEEP)
    {
        if (param.long_press_millisecond >= POWER_ON_MS)
        {
            LOG("system_into_poweron, %d", param.long_press_millisecond);
            power_on();
            motor_start(500);
            display_show(view_power_on_animation, DISP_MODE_NORMAL);
        }
        return true;
    }
    return false;
#else
    if (param.event == BUTTON_EVENT_LONG_PRESS
#ifdef USE_TOUCHPAD
        || param.event == BUTTON_EVENT_TP_TAP_LONG
#endif
    )
    {
        power_on();
        motor_start(500);
        display_show(view_power_on_animation, DISP_MODE_NORMAL);
        return true;
    }
    return false;
#endif
}

void view_untested_display(void)
{
    	#warning TODO:
}
void view_screen_test_display(void)
{
    disp_draw_rect_color(0, 0, OLED_SIZE_X - 1, OLED_SIZE_Y - 1, RGB565_COLOR_WHITE);
}
bool view_screen_test_event(event_param_t param)
{
    switch (param.event)
    {
    case BUTTON_EVENT_SHORT_PRESS:
        display_show(view_time, DISP_MODE_DOWN_MOVE);
        return true;
    }
    return false;
}
void view_heart_rate_test_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        disp_ctrl.com_data.i_temp[0] = 30 - disp_ctrl.auto_refresh_count / 2;
        disp_ctrl.com_data.i_temp[2] = APPhealth_Type;
        if (disp_ctrl.com_data.i_temp[0] < 0)
        {
            disp_ctrl.com_data.i_temp[0] = 0;
        }
        if (disp_ctrl.auto_refresh_count > 35 * 2)
        {
            // LOG("test timeout [%d]", time);
            display_show_type_recover();
            return;
        }
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
        if (!disp_ctrl.com_data.i_temp[1])
        {
            //!未佩戴
            if (disp_ctrl.com_data.i_temp[4] == -1)
            {
                disp_ctrl.auto_refresh_count = disp_ctrl.auto_refresh_count;
                LOG("no wear start time [%d]", disp_ctrl.auto_refresh_count);
            }
            else
            {
                if (disp_ctrl.auto_refresh_count - disp_ctrl.com_data.i_temp[4] > 3 * 2)
                {
                    LOG("no wear timeout [%d]", disp_ctrl.auto_refresh_count - disp_ctrl.com_data.i_temp[4]);
                    display_show_type_recover();
                    return;
                }
            }
        }
        else
        {
            disp_ctrl.com_data.i_temp[4] = -1;
        }
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[6] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[6] %= 12;
            if (disp_ctrl.com_data.i_temp[6] == 0)
            {
                disp_ctrl.com_data.i_temp[6] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[7] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
    }
    	#warning TODO:
}
bool view_heart_rate_test_event(event_param_t param)
{
    switch (param.event)
    {
    case event_view_entry:
        disp_ctrl.auto_close_delay = 40000;
        return true;
    case event_view_exit:
        APPhealth_Stop();
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return true;
    case event_ble_disconnect:
        // case event_move_level_up:
        display_show_type_recover();
        LOG("view_heart_rate_test cancel");
        return true;
    }
    return false;
}
#ifdef AXILLARY_TEMPERATURE
void view_axillary_test_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        disp_ctrl.com_data.i_temp[0] = axillary_measure_duration();
        float axillary = get_axillary_temperature();
        disp_ctrl.com_data.i_temp[1] = axillary * 100;
    }
    char buff[128] = {0};
    sprintf(buff, "腋温 %d %d.%d", disp_ctrl.com_data.i_temp[0], disp_ctrl.com_data.i_temp[1] / 100, disp_ctrl.com_data.i_temp[1] % 100);
    disp_show_font_string_utf8_mid(100, buff, 0);
}
bool view_axillary_test_event(event_param_t param)
{
    switch (param.event)
    {
        // case event_view_entry:
        //     // disp_ctrl.auto_close_delay = 40000;
        //     return true;
        // case event_view_exit:
        //     APPhealth_Stop();
        //     hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        //     return true;
        // case event_ble_disconnect:
        //     // case event_move_level_up:
        //     // display_show_type_recover();
        //     // LOG("view_heart_rate_test cancel");
        //     return true;
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
    case event_button_front_long_press:
#endif
        return true;
    }
    return false;
}
#endif //#ifdef AXILLARY_TEMPERATURE
void view_heart_rate_result_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;

        disp_ctrl.com_data.i_temp[0] = app_health_test_result[0];
        disp_ctrl.com_data.i_temp[1] = app_health_test_result[1];
        disp_ctrl.com_data.i_temp[2] = APPhealth_Type;
        if (disp_ctrl.auto_refresh_count > 5 * 2)
        {
            // LOG("test result timeout [%d]", time);
            display_show_type_recover();
            return;
        }
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
    }
    	#warning TODO:
}

bool view_heart_rate_test_result_event(event_param_t param)
{
    switch (param.event)
    {
    case event_view_entry:
        disp_ctrl.auto_close_delay = 10000;
        stopwatch_start();
        LOG("view_heart_rate_test entry");
        return true;
    case event_view_exit:
        stopwatch_stop();
        LOG("view_heart_rate_test exit");
        return true;
    }
    return false;
}
bool view_system_display(int view)
{
    switch (view)
    {
    case view_heart_rate_test:
        view_heart_rate_test_display();
        return true;
    case view_heart_rate_result:
        view_heart_rate_result_display();
        return true;
#ifdef AXILLARY_TEMPERATURE
    case view_axillary_test:
        view_axillary_test_display();
        return true;
#endif
    case view_power_on_animation:
        view_power_on_display();
        return true;
    case view_power_off_animation:
        view_power_off_display();
        return true;
    case view_untested:
        view_untested_display();
        return true;
    case view_screen_test:
        view_screen_test_display();
        return true;
    }
    return false;
}
bool view_system_event(int view, event_param_t param)
{
    bool result = false;
    switch (view)
    {
    case view_screen_test:
        result = view_screen_test_event(param);
        break;
    case view_heart_rate_test:
        result = view_heart_rate_test_event(param);
        break;
    case view_heart_rate_result:
        result = view_heart_rate_test_result_event(param);
        break;
#ifdef AXILLARY_TEMPERATURE
    case view_axillary_test:
        result = view_axillary_test_event(param);
        break;
#endif
    }
    return result;
}
